//
//  WBBaseViewController.swift
//  小荣荣新浪微博
//
//  Created by zzr on 2018/4/19.
//  Copyright © 2018年 zzr. All rights reserved.
//

import UIKit



class WBBaseViewController: UIViewController {

    //用户登录标记
    var userLogon = false
    
    //访客视图信息
    var visitorInfoDict: [String:String]?
    
    var tableView: UITableView?
    //刷新控件
    var refreshControl: UIRefreshControl?
    
    //上拉刷新
    var isPullup = false
    
    //自定义导航条
    lazy var navigationBar = UINavigationBar(frame: CGRect(x: 0, y: kStatusBarH, width: kScreenW, height: kNavigationBarH))
    lazy var navItem = UINavigationItem()
    
    override func viewDidLoad() {
        super.viewDidLoad()

       setupUI()
       loadData()
        
    }

    //MARK:- 设置界面
    func setupUI() {
        view.backgroundColor = UIColor.randomColor()
        
        //取消自动缩进
        automaticallyAdjustsScrollViewInsets = false
        
        setupNavigationBar()
        userLogon ? setupTableView() : setupVisitorView()
        
    }
    
    private func setupNavigationBar() {
        view.addSubview(navigationBar)
        navigationBar.items = [navItem]
        
        navItem.titleView = UILabel.navigationItemTitleLabel(title: self.title ?? "")
        //设置navigation颜色
        navigationBar.barTintColor = UIColor(red: 235/255, green: 230/255, blue: 230/255, alpha: 1)
        //navigationBar.titleTextAttributes = [ForegroundColorAttributeName: UIColor.whiteColor()]
    }
    
    private func setupTableView() {
        tableView = UITableView(frame: view.bounds, style: .plain)
        view.insertSubview(tableView!, belowSubview: navigationBar)
        
        //设置数据源和代理，子类实现数据源方法
        tableView?.delegate = self
        tableView?.dataSource = self

        //设置内容缩进
        tableView?.contentInset = UIEdgeInsets(top: navigationBar.bounds.height, left: 0, bottom: 0, right: 0)
        //tabBarController?.tabBar.bounds.height
        
        //设置刷新控件
        refreshControl = UIRefreshControl()
        tableView?.addSubview(refreshControl!)
        //添加监听方法
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
        
    }
    
    private func setupVisitorView() {
        let visitorView = WBVisitorView(frame: view.bounds)
        
        view.insertSubview(visitorView, belowSubview: navigationBar)
        
        visitorView.visitorInfo = visitorInfoDict
    }
    
    @objc func loadData() {
        refreshControl?.endRefreshing()
    }
}


extension WBBaseViewController: UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        //1.判断indexpath是否最后一行
        let row = indexPath.row
        //2.取最大的section的最后一行
        let section = tableView.numberOfSections - 1
        
        if row < 0 || section < 0 {
            return
        }
        //行数
        let count = tableView.numberOfRows(inSection: section)
    
        if row == (count - 1) && !isPullup {
            print("上拉刷新")
            isPullup = true
            //开始刷新
            loadData()
        }
        
    }
}

extension WBBaseViewController: UITableViewDelegate {
    
}
